<?php echo $this->element('displayHeader'); ?>
<?php echo $ajax->form('MessageSent', 'post', array('update'=>'content', 'indicator' => 'indicator', 'url'=>array('action'=>'send'))); ?>
<div class="toolbar">
    <div id="tLeft">
        <?php if ($mode == 'add') : ?>
            <?php echo $this->element('toolbar', array('buttons' => array(
                    array('save' => array('icon' => 'send_message', 'text' => 'Send Message', 'message' => 'Do you want to send message?')),
                    'clear', 'back'))); ?>
        <?php else : // mode edit ?>
            <?php echo $this->element('toolbar', array('buttons' => array('add',
                    array('delete' => array('model' => 'Message', 'title' => 'subject')), 'back'))); ?>
        <?php endif; ?>
    </div>
    <div id="tRight"><?php echo $this->element('neighbors', array('model' => 'Message', 'title' => 'subject')); ?></div>
</div>

<?php echo $form->input("MessageSent.id"); ?>
<?php if (isset($reply_id)) : ?>
    <?php echo $form->hidden("reply_id", array('value' => $reply_id)); ?>
<?php endif; ?>
<fieldset>
    <table>
        <tbody>
            <tr>
                <td width="100px" class="top"><?php echo $form->label("MessageSent.receiver", "Send to", array('class' => 'required')) ?></td>
                <td>
                    <?php echo $form->radio('MessageSent.receiver', array(CUST_PREFIX => 'Customer', SUPPLIER_PREFIX => 'Vendor'), array('legend' => false)); ?><br />
                    <?php echo $form->hidden("MessageSent.receiver_id"); ?>
                    <?php echo $form->select("MessageSent.supplier_id", $suppliers, null, array('style' => 'display: none;')); ?>
                    <?php echo $form->select("MessageSent.customer_id", $customers, null, array('style' => 'display: none;')); ?>
                </td>
            </tr>
            <?php if ($mode == 'add') : ?>
            <tr>
                <td><?php echo $form->label("MessageSent.send_to", "Attn.", array('class' => 'required')) ?></td>
                <td><?php echo $form->text("MessageSent.send_to") ?> </td>
            </tr>
            <tr id="trReceiverUser">
                <td><?php echo $form->label("MessageSent.receiver_user_id", "User", array('class' => 'required')) ?></td>
                <td>
                        <?php echo $form->select("MessageSent.receiver_user_id") ?>
                </td>
            </tr>
            <?php endif; ?>
            <tr>
                <td><?php echo $form->label("MessageSent.subject", "Subject", array('class' => 'required')) ?></td>
                <td>
                    <?php if ($mode == 'add') : ?>
                        <?php echo $form->text("MessageSent.subject", array('size' => 80)) ?>
                    <?php else : ?>
                        <?php echo $message['Message']['subject']; ?>
                    <?php endif; ?>
                </td>
            </tr>
        </tbody>
    </table>
</fieldset>
<fieldset>
    <?php if ($mode == 'add') : ?>
    <div><?php  $bodyId = $this->uuid('div', 'message/view/' . rand());
            echo $form->textarea('MessageSent.body', array('id' => $bodyId, 'rows' => 10)); ?></div>
    <?php else : ?>
    <div class="messageBody"><?php echo $message['Message']['body']; ?></div>
    <?php endif; ?>
</fieldset>

<?php echo $form->end(); ?>
<?php if ($mode == 'add') : ?>
<script type="text/javascript">
    $j(function() {
        // การเลือก send to ว่า Supplier/Customer
        $j(":radio[id*=Receiver]").bind('click', function() {
            if ($j(this).is(':checked')) {
                var receiver = $j(this).val();
            } else {
                var receiver = null;
            }
            $j('#MessageSentCustomerId').hide();
            $j('#MessageSentSupplierId').hide();
            if (receiver == 'customer') {
                $j('#MessageSentCustomerId').show();
            } else if (receiver == 'supplier') { // 'B''
                $j('#MessageSentSupplierId').show();
            }
        });
        // reply
        if ($j(":radio[id*=Receiver]:checked").get(0) != null) {
            $j(":radio[id*=Receiver]:checked").click();
        }

        // กำหนด receiver_id ตาม ผู้รับ Supplier/Customer
        $j('#MessageSentSupplierId, #MessageSentCustomerId').bind('change', function() {
            var receiver_id = $j(this).val();
            $j('#MessageSentReceiverId').val(receiver_id).change();
        });

        $j('#MessageSentReceiverId').bind('change', function() {
            var send_to = $j(':radio').val();
            var val = $j(this).val();
            if (val != '') {
                if (send_to == 'customer') {

                } else { // vendor

                }
            }
        });

        $j('.send_message').bind('click', function() {
            $j('#<?php echo $bodyId; ?>').val(editor.getData());
        });
    })

    ckeditor('<?php echo $bodyId; ?>');
</script>
<?php endif; ?>

